﻿using System;
using System.Web.UI.WebControls;

using System.Text;

namespace IFD2
{
    public partial class NewOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //form1.DefaultButton = butFltQuery.ID;
            if (IsPostBack)
            {
                lblError.Text = "";
                return;
            }
            else
            {
                if (Session["ACL"].ToString().IndexOf("SA3") < 0)
                    Response.Redirect("unauthorized.aspx");
            }

            txtOri1.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOri1',event)");
            txtDes1.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDes1',event)");
            txtOri2.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOri2',event)");
            txtDes2.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDes2',event)");
            txtOri3.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOri3',event)");
            txtDes3.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDes3',event)");
            txtOri4.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOri4',event)");
            txtDes4.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDes4',event)");

            txtOri1.Attributes.Add("onkeydown", "key_down(event)");
            txtDes1.Attributes.Add("onkeydown", "key_down(event)");
            txtOri2.Attributes.Add("onkeydown", "key_down(event)");
            txtDes2.Attributes.Add("onkeydown", "key_down(event)");
            txtOri3.Attributes.Add("onkeydown", "key_down(event)");
            txtDes3.Attributes.Add("onkeydown", "key_down(event)");
            txtOri4.Attributes.Add("onkeydown", "key_down(event)");
            txtDes4.Attributes.Add("onkeydown", "key_down(event)");

            txtOri1.Attributes.Add("autocomplete", "off");
            txtDes1.Attributes.Add("autocomplete", "off");
            txtOri2.Attributes.Add("autocomplete", "off");
            txtDes2.Attributes.Add("autocomplete", "off");
            txtOri3.Attributes.Add("autocomplete", "off");
            txtDes3.Attributes.Add("autocomplete", "off");
            txtOri4.Attributes.Add("autocomplete", "off");
            txtDes4.Attributes.Add("autocomplete", "off");
        }

        protected void butFltQuery_Click(object sender, EventArgs e)
        {
            if (lstRequestType.SelectedValue == "B" && Session["ACL"].ToString().IndexOf("BD2") < 0)
            {
                lblError.Text = "No authority to block request!";
                return;
            }
            if(save_session())
                Response.Redirect("AvailFlights.aspx?request_type=" + lstRequestType.SelectedValue/*+"&is_group="+chkIsGroup.Checked.ToString()*/);

        }

        protected void btnFareQuery_Click(object sender, EventArgs e)
        {
            if (lstRequestType.SelectedValue == "B" && Session["ACL"].ToString().IndexOf("BD2") < 0)
            {
                lblError.Text = "No authority to block request!";
                return;
            }
            if(save_session())
                Response.Redirect("AvailFares.aspx?request_type=" + lstRequestType.SelectedValue/*+"&is_group="+chkIsGroup.Checked.ToString()*/);
        }

        protected void radSegCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            int segCount = Int16.Parse(radSegCount.SelectedValue.TrimEnd());
            if (segCount == 1)
            {
                txtOri1.Visible = true;
                txtDes1.Visible = true;
                txtFltDate1.Visible = true;
                txtOri2.Visible = false;
                txtDes2.Visible = false;
                txtFltDate2.Visible = false;
                txtOri3.Visible = false;
                txtDes3.Visible = false;
                txtFltDate3.Visible = false;
                txtOri4.Visible = false;
                txtDes4.Visible = false;
                txtFltDate4.Visible = false;

            }
            else if (segCount == 2)
            {
                txtOri1.Visible = true;
                txtDes1.Visible = true;
                txtFltDate1.Visible = true;
                txtOri2.Visible = true;
                txtDes2.Visible = true;
                txtFltDate2.Visible = true;
                txtOri3.Visible = false;
                txtDes3.Visible = false;
                txtFltDate3.Visible = false;
                txtOri4.Visible = false;
                txtDes4.Visible = false;
                txtFltDate4.Visible = false;

            }
            else if (segCount == 3)
            {
                txtOri1.Visible = true;
                txtDes1.Visible = true;
                txtFltDate1.Visible = true;
                txtOri2.Visible = true;
                txtDes2.Visible = true;
                txtFltDate2.Visible = true;
                txtOri3.Visible = true;
                txtDes3.Visible = true;
                txtFltDate3.Visible = true;
                txtOri4.Visible = false;
                txtDes4.Visible = false;
                txtFltDate4.Visible = false;

            }
            else if (segCount == 4)
            {
                txtOri1.Visible = true;
                txtDes1.Visible = true;
                txtFltDate1.Visible = true;
                txtOri2.Visible = true;
                txtDes2.Visible = true;
                txtFltDate2.Visible = true;
                txtOri3.Visible = true;
                txtDes3.Visible = true;
                txtFltDate3.Visible = true;
                txtOri4.Visible = true;
                txtDes4.Visible = true;
                txtFltDate4.Visible = true;

            }
        }

        protected bool save_session()
        {
            int trip_count, adt, chd, inf;
            TextBox[] txt_ori = new TextBox[4];
            txt_ori[0] = txtOri1;
            txt_ori[1] = txtOri2;
            txt_ori[2] = txtOri3;
            txt_ori[3] = txtOri4;
            TextBox[] txt_des = new TextBox[4];
            txt_des[0] = txtDes1;
            txt_des[1] = txtDes2;
            txt_des[2] = txtDes3;
            txt_des[3] = txtDes4;
            TextBox[] txt_date = new TextBox[4];
            txt_date[0] = txtFltDate1;
            txt_date[1] = txtFltDate2;
            txt_date[2] = txtFltDate3;
            txt_date[3] = txtFltDate4;

            //trip_count = Convert.ToInt16(dlTripCount.SelectedValue);
            trip_count = Convert.ToInt16(radSegCount.SelectedValue.TrimEnd());
            Session["TRIP_COUNT"] = trip_count;
            Session["REQUEST_TYPE"] = lstRequestType.SelectedValue;

            adt = Convert.ToInt16(txtAdtCount.Text);
            chd = Convert.ToInt16(txtChdCount.Text);
            inf = Convert.ToInt16(txtInfCount.Text);
            if (adt < 0 || chd < 0 || inf < 0 || (adt < inf) || adt + chd > 50)
            {
                lblError.Text = "Illegal Passenger Count!";
                return false;
            }
            Session["ADT_COUNT"] = adt;
            Session["CHD_COUNT"] = chd;
            Session["INF_COUNT"] = inf;
            Session["SEATS"] = adt + chd;

            StringBuilder buf = new StringBuilder();
            buf.Clear();
            DateTime last_date = Convert.ToDateTime("1999-12-31");
            DateTime curr_date;
            for (int i = 1; i <= trip_count; i++)
            {
                try
                {
                    DateTime.Parse(txt_date[i - 1].Text);
                }
                catch
                {
                    lblError.Text = "Invalid date format!";
                    return false;
                }
                curr_date = Convert.ToDateTime(txt_date[i - 1].Text);
                if (last_date > curr_date)
                {
                    lblError.Text = "Flight dates are disorder!";
                    return false;
                }
                if (txt_ori[i - 1].Text.TrimEnd() == "" || txt_des[i - 1].Text.TrimEnd() == "")
                {
                    lblError.Text = "Please input trip info!";
                    return false;
                }
                Session["ORI_" + i.ToString()] = txt_ori[i - 1].Text.ToUpper();//开始ORI_n、DES_n是City，最后提交时变为Airport！
                Session["DES_" + i.ToString()] = txt_des[i - 1].Text.ToUpper();
                Session["FLT_DATE_" + i.ToString()] = curr_date.ToLongDateString();

                last_date = curr_date;
            }
            return true;
        }
    }
}